package org.realogic.cfm.persistence.mybatis.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

public class KLikeTypeHandler extends BaseTypeHandler<String> {

	@Override
	public String getNullableResult(ResultSet rs, String columnName) 
			throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public String getNullableResult(ResultSet rs, int columnIndex) 
			throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public String getNullableResult(CallableStatement cs, int columnIndex) 
			throws SQLException {
		throw new UnsupportedOperationException();
	}

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) 
			throws SQLException {
		if (parameter != null) {
			 if (jdbcType == JdbcType.VARCHAR) {
				String value = "%" + parameter + "%";
				ps.setString(i, value);
			} else {
				ps.setString(i, parameter);
			}
		}
	}
}
